package com.ux.sns
{
	import com.ux.base.BLoader;
	import com.ux.event.YouTubeEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Security;

	
	
	[Event(name="playerLoadComplete", type="com.ux.event.YouTubeEvent")]
	[Event(name="ready", type="com.ux.event.YouTubeEvent")]
	[Event(name="stateChange", type="com.ux.event.YouTubeEvent")]
	[Event(name="playbackQualityChange", type="com.ux.event.YouTubeEvent")]
	[Event(name="error", type="com.ux.event.YouTubeEvent")]
	
	/**
	 * youtube api : http://code.google.com/intl/ko/apis/youtube/flash_api_reference.html
	 * @author winnie
	 */	
	public class YouTubePlayer extends Sprite
	{
		public static const UNSTARTED : int = -1;
		public static const ENDED : int = 0;
		public static const PLAYING : int = 1;
		public static const PAUSED : int = 2;
		public static const BUFFERING : int = 3;
		public static const VIDEO_CUED : int = 5;
		
		public static const QUALITY_SMALL : String = "small";
		public static const QUALITY_MEDIUM : String = "medium";
		public static const QUALITY_LARGE : String = "large";
		public static const QUALITY_HD720 : String = "hd720";
		public static const QUALITY_HD1080 : String = "hd1080";
		public static const QUALITY_HIGHRES : String = "highres";
		
		private var player : Object;
		private var loader : BLoader;
		private var playerURL : String = "http://www.youtube.com/apiplayer?version=3";
		
		public function YouTubePlayer()
		{
			super();
			
			Security.allowDomain("*");
			this.loadPlayer();
		}
		
		public function loadPlayer():void
		{
			if( loader != null ) return; 
			
//			trace(" ==> YouTubePlayer loadPlayer : 플레이어 로드해라.");

			loader = new BLoader( playerURL, this, function():void
			{
				player = loader.loadData;
				player.addEventListener("onReady", onPlayerReady);
				player.addEventListener("onError", onPlayerError);
				player.addEventListener("onStateChange", onPlayerStateChange);
				player.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
			});
		}
		
		
		
		
		// event 
		
		private function onPlayerReady( e:Event ):void
		{
//			trace(" ==> youTubePlayerApiTest onPlayerReady : " + Object(e).data);
			this.setSize(320, 240);
			dispatchEvent( new YouTubeEvent( YouTubeEvent.READY, Object(e).data ) );
		}
		
		
		
		
		/**
		 * <br> This event is fired when an error in the player occurs. </br>
		 * <br> The possible error codes are 100, 101, and 150. </br>
		 * <br> The 100 error code is broadcast when the video requested is not found. </br>
		 * <br> This occurs when a video has been removed (for any reason), </br>
		 * <br> or it has been marked as private. </br>
		 * <br> The 101 error code is broadcast when the video requested does not allow </br>
		 * <br> playback in the embedded players. </br>
		 * <br> The error code 150 is the same as 101, it's just 101 in disguise!</br>
		 */		
		private function onPlayerError( e:Event ):void
		{
//			trace(" ==> youTubePlayerApiTest onPlayerError : " + Object(e).data);
			dispatchEvent( new YouTubeEvent( YouTubeEvent.ERROR, Object(e).data ) );
		}
		
		private function onPlayerStateChange( e:Event ):void
		{
//			trace(" ==> youTubePlayerApiTest onPlayerStateChange : " + Object(e).data );
			dispatchEvent( new YouTubeEvent( YouTubeEvent.STATE_CHANGE, Object(e).data ) );
		}
		
		private function onVideoPlaybackQualityChange( e:Event ):void
		{
//			trace(" ==> youTubePlayerApiTest onVideoPlaybackQualityChange : " + Object(e).data);
			dispatchEvent( new YouTubeEvent( YouTubeEvent.PLAYBACK_QUALITY_CHANGE, Object(e).data ) );
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//
		// api
		//
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// function 
		//
		//-----------------------------------------------------------------------------------
		
		
		
		// Queueing functions
		
		public function cueVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):void
		{
			player.cueVideoById(videoId, startSeconds, suggestedQuality);
		}
		
		public function loadVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):void
		{
			player.loadVideoById(videoId, startSeconds, suggestedQuality);
		}
		
		public function cueVideoByUrl(mediaContentUrl:String, startSeconds:Number):void
		{
			player.cueVideoByUrl(mediaContentUrl, startSeconds);
		}
		
		public function loadVideoByUrl(mediaContentUrl:String, startSeconds:Number):void
		{
			player.loadVideoByUrl(mediaContentUrl, startSeconds)
		}
		
		
		
		
		
		
		// Playback controls and player settings
		
		public function playVideo():void
		{
			player.playVideo();
		}
		
		public function pauseVideo():void
		{
			player.pauseVideo();
		}
		
		public function stopVideo():void
		{
			player.stopVideo();
		}
		
		
		
		
		/**
		 * 
		 * @param seconds seek time
		 * @param allowSeekAhead - if dragging : false, release : true
		 * 
		 */		
		public function seekTo(seconds:Number, allowSeekAhead:Boolean):void
		{
			player.seekTo( seconds, allowSeekAhead );
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		// Changing the player volume
		
		public function mute():void
		{
			player.mute();
		}
		
		public function unMute():void
		{
			player.unMute();
		}
		
		public function isMuted():Boolean
		{
			return player.isMuted();
		}
		
		public function setVolume(volume:Number):void
		{
			player.setVolume(volume);
		}
		
		public function getVolume():Number
		{
			return player.getVolume();
		}
	
		
		
		
		
		
		
		
		
		
		
		// Setting the player size
		public function setSize(width:Number, height:Number):void
		{
			player.setSize(width, height);
		}
		
		
		
		
		
		
		
		
		
		
		// Playback status
		
		public function getVideoBytesLoaded():Number
		{
			return player.getVideoBytesLoaded();
		}
		
		public function getVideoBytesTotal():Number
		{
			return player.getVideoBytesTotal();
		}
		
		public function getVideoStartBytes():Number
		{
			return player.getVideoStartBytes();
		}
		
		public function getPlayerState():Number
		{
			return player.getPlayerState();
		}
		
		public function getCurrentTime():Number
		{
			return player.getCurrentTime();
		}
		
		
		
		
		
		
		
		
		
		
		
		
		// Playback quality
		
		public function getPlaybackQuality():String
		{
			return player.getPlaybackQuality();
		}
		
		/**
		 * 
		 * <br> QUALITY_SMALL </br>
		 * <br> QUALITY_MEDIUM </br>
		 * <br> QUALITY_LARGE </br>
		 * <br> QUALITY_HD720 </br>
		 * <br> QUALITY_HD1080 </br>
		 * <br> QUALITY_HIGHRES </br>
		 * @param suggestedQuality display Quality but not apply
		 * 
		 */		
		public function setPlaybackQuality(suggestedQuality:String):void
		{
			player.setPlaybackQuality(suggestedQuality);
		}
		
		public function getAvailableQualityLevels():Array
		{
			return player.getAvailableQualityLevels();
		}
		
		
		
		
		
		
		
		
		
		
		
		// Retrieving video information
		
		public function getDuration():Number
		{
			return player.getDuration();
		}
		
		public function getVideoUrl():String
		{
			return player.getVideoUrl();
		}
		
		public function getVideoEmbedCode():String
		{
			return player.getVideoEmbedCode();
		}
		
		
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// Special Functions
		//
		//-----------------------------------------------------------------------------------
		
		public function destroy():void
		{
			player.destroy();
			
			if( this.parent )
			{
				parent.removeChild( this );
			}
		}
	}
}